#   Copyright (c)  鱼肠的渔场小组  2025-2025 . All rights reserved.
#   TradingPlatform_Server is licensed under Mulan PSL v2.
#   You can use this software according to the terms and conditions of the Mulan
#   PSL v2.
#   You may obtain a copy of Mulan PSL v2 at:
#            http://license.coscl.org.cn/MulanPSL2
#   THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY
#   KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
#   NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
#   See the Mulan PSL v2 for more details.
import os
import uuid

from django.db import models

from Server.enum import StoreState, TakeoutState, RemarkChoice, DeliveryState, OrderState, PaymentMethod

# Create your models here.
"""
max_length:最大长度
verbose_name:字段说明
"""


def user_directory_path(instance, filename):
    ext = filename.split('.')[-1]
    filename = f'{uuid.uuid4()}.{ext}'
    return os.path.join('users', str(instance.id), filename)


def takeout_directory_path(instance, filename):
    ext = filename.split('.')[-1]
    filename = f'{uuid.uuid4()}.{ext}'
    return os.path.join('takeouts', str(instance.id), filename)


"""
---------------------------------------------------------用户模型----------------------------------------------------------------
"""


class User(models.Model):
    id = models.CharField(max_length=100, unique=True, verbose_name='用户唯一标识符', primary_key=True, db_index=True)
    username = models.CharField(max_length=100, verbose_name='用户昵称', null=False, default='匿名用户')
    password = models.CharField(max_length=200, verbose_name='密码', null=False)
    phone = models.CharField(max_length=100, verbose_name='手机号码', null=False, unique=True, db_index=True)
    head_portrait = models.ImageField(upload_to=user_directory_path, verbose_name='用户头像',
                                      default='users/default/user_portrait.png')
    is_admin = models.BooleanField(default=False, verbose_name='是否为管理员')
    money = models.FloatField(default=100.0, verbose_name="个人资金")

    def __str__(self):
        return self.id


"""
---------------------------------------------------------地址模型-----------------------------------------------------------------
"""


class Address(models.Model):
    id = models.AutoField(primary_key=True, verbose_name='地址唯一标识符', db_index=True)
    school_name = models.CharField(max_length=200, verbose_name='学校名称', null=False)
    campus = models.CharField(max_length=100, verbose_name='校区', null=False)
    address = models.CharField(max_length=200, verbose_name='详细地址', null=False)
    state = models.BooleanField(verbose_name='是否为默认地址', null=False, default=False)
    user = models.ForeignKey(to="User", on_delete=models.SET_NULL, null=True, db_index=True,
                             verbose_name='用户唯一标识符')


"""
---------------------------------------------------------商家模型-----------------------------------------------------------------
"""


class Store(models.Model):
    STORE_CHOICES = (
        ('auditing', StoreState.auditing),
        ('audited', StoreState.audited),
        ('forbidden', StoreState.forbidden),
        ('deleted', StoreState.deleted),
        ('reject', StoreState.reject)
    )
    id = models.CharField(max_length=100, unique=True, verbose_name='商家唯一标识符', primary_key=True, db_index=True)
    store_name = models.CharField(max_length=100, verbose_name='商家名称', null=False)
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='商家创建时间')
    address = models.CharField(max_length=200, verbose_name='商家详细地址', null=False)
    takeout_count = models.IntegerField(verbose_name='商家外卖数量', null=False, default=0)
    state = models.IntegerField(verbose_name='商家状态', null=True, choices=STORE_CHOICES)
    user = models.OneToOneField(to="User", on_delete=models.SET_NULL, null=True, db_index=True,
                                verbose_name='用户唯一标识符')
    introduce = models.CharField(verbose_name='店铺介绍', default='店铺的主人很懒，什么也没有留下～', max_length=1024,
                                 null=False)


"""
---------------------------------------------------------订单模型-----------------------------------------------------------------
"""


class Order(models.Model):
    ORDER_CHOICES = (
        ('unfinished', OrderState.unfinished),
        ('finished', OrderState.finished)
    )
    PAYMENT_CHOICES = (
        ('wechat', PaymentMethod.wechat),
        ('alipay', PaymentMethod.alipay)
    )
    id = models.AutoField(verbose_name='订单唯一标识符', primary_key=True, db_index=True)
    order_id = models.CharField(max_length=100,  verbose_name='订单唯一标识符', db_index=True)
    price = models.FloatField(verbose_name='交易金额', null=False)
    count = models.IntegerField(verbose_name='交易数量', null=False)
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='订单创建时间')
    finish_time = models.DateTimeField(auto_now_add=True, verbose_name='订单完成时间')
    state = models.IntegerField(verbose_name='订单状态', null=False, choices=ORDER_CHOICES)
    note = models.CharField(max_length=300, verbose_name='备注', null=True)
    payment_method = models.IntegerField(verbose_name='支付方式', null=False, choices=PAYMENT_CHOICES)
    user = models.ForeignKey(to="User", on_delete=models.SET_NULL, null=True, db_index=True,
                             verbose_name='用户唯一标识符')
    store = models.ForeignKey(to="Store", on_delete=models.SET_NULL, null=True, db_index=True,
                              verbose_name='商家唯一标识符')
    address = models.ForeignKey(to="Address", verbose_name='地址唯一标识符', null=True, on_delete=models.SET_NULL)
    takeout = models.ForeignKey(to="Takeout", on_delete=models.SET_NULL, null=True, db_index=True,
                                verbose_name="外卖唯一标识符")


"""
---------------------------------------------------------支付模型-----------------------------------------------------------------
"""


class Payment(models.Model):
    id = models.AutoField(primary_key=True, db_index=True, verbose_name='支付唯一标识符')
    pay_time = models.DateTimeField(auto_now_add=True, verbose_name='支付时间')
    price = models.FloatField(verbose_name='支付金额', null=False)
    user = models.ForeignKey(to="User", on_delete=models.SET_NULL, null=True, db_index=True,
                             verbose_name='用户标识符')
    store = models.ForeignKey(to="Store", on_delete=models.SET_NULL, null=True, db_index=True,
                              verbose_name='商家标识符')
    order = models.ForeignKey(to="Order", on_delete=models.SET_NULL, null=True, db_index=True,
                              verbose_name='订单标识符')


"""
---------------------------------------------------------配送模型-----------------------------------------------------------------
"""


class Delivery(models.Model):
    DELIVERY_CHOICES = (
        ('get_takeout', DeliveryState.get_takeout),
        ('delivering', DeliveryState.delivering),
        ('finish', DeliveryState.finish),
        ('overtime', DeliveryState.overtime)
    )
    REMARK_CHOICES = (
        ('zero', RemarkChoice.zero),
        ('one', RemarkChoice.one),
        ('two', RemarkChoice.two),
        ('three', RemarkChoice.three),
        ('four', RemarkChoice.four),
        ('five', RemarkChoice.five)
    )
    id = models.AutoField(primary_key=True, db_index=True, verbose_name='配送唯一标识符')
    price = models.FloatField(verbose_name='配送费', null=False, default=2.5)
    state = models.IntegerField(verbose_name='配送状态', null=False, choices=DELIVERY_CHOICES)
    remark = models.IntegerField(verbose_name='评分', null=False, choices=REMARK_CHOICES)
    order = models.ForeignKey(to="Order", on_delete=models.SET_NULL, null=True, db_index=True,
                              verbose_name='订单唯一标识符')
    user = models.ForeignKey(to="User", on_delete=models.SET_NULL, null=True, db_index=True,
                             verbose_name="用户唯一标识符")


"""
---------------------------------------------------------外卖模型-----------------------------------------------------------------
"""


class Takeout(models.Model):
    TAKE_OUT_CHOICES = (
        ('auditing', TakeoutState.auditing),
        ('audited', TakeoutState.audited),
        ('forbidden', TakeoutState.forbidden),
        ('deleted', TakeoutState.deleted),
        ('reject', TakeoutState.reject)
    )
    id = models.AutoField(primary_key=True, verbose_name='外卖唯一标识符')
    state = models.SmallIntegerField(verbose_name='外卖状态', null=False, choices=TAKE_OUT_CHOICES)
    takeout_name = models.CharField(max_length=200, verbose_name='外卖名称', null=False)
    introduce = models.CharField(max_length=200, verbose_name='简介', null=False)
    takeout_portrait = models.ImageField(upload_to='takeout', verbose_name='图片',
                                         default='takeouts/default/takeout_portrait.png')
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='上架时间')
    last_update_time = models.DateTimeField(auto_now=True, verbose_name='最近修改时间')
    price = models.FloatField(verbose_name='价格', null=False)
    flow = models.IntegerField(verbose_name='流量', null=False, default=0)
    mark = models.FloatField(verbose_name='评分', null=False, default=0)
    remark_count = models.IntegerField(verbose_name='评论数量', null=False, default=0)
    store = models.ForeignKey(to="Store", on_delete=models.SET_NULL, null=True, db_index=True,
                              verbose_name='商家唯一标识符')
    kind = models.ForeignKey(to="Kind", on_delete=models.SET_NULL, null=True, db_index=True,
                             verbose_name='类型唯一标识符')

    def __str__(self):
        return self.id


"""
---------------------------------------------------------评论模型-----------------------------------------------------------------
"""


class Discuss(models.Model):
    id = models.AutoField(primary_key=True, db_index=True, verbose_name='评论唯一标识符')
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='评论时间')
    content = models.CharField(max_length=1000, null=False, verbose_name='用户评论内容')
    user = models.ForeignKey(to="User", on_delete=models.SET_NULL, null=True, db_index=True,
                             verbose_name='用户唯一标识符')
    takeout = models.ForeignKey(to="Takeout", on_delete=models.SET_NULL, null=True, db_index=True,
                                verbose_name='外卖唯一标识符')


"""
---------------------------------------------------------菜单模型-----------------------------------------------------------------
"""


class Menu(models.Model):
    id = models.AutoField(primary_key=True, db_index=True, verbose_name='菜单唯一标识符')
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='加入菜单时间')
    user = models.ForeignKey(to="User", verbose_name='用户唯一标识符', on_delete=models.SET_NULL, null=True,
                             db_index=True)
    count = models.IntegerField(verbose_name='数量', null=False)
    price = models.FloatField(verbose_name='价格', null=False)
    takeout = models.ForeignKey(to="Takeout", on_delete=models.SET_NULL, null=True, db_index=True,
                                verbose_name='外卖唯一标识符')


"""
---------------------------------------------------------类别模型-----------------------------------------------------------------
"""


class Kind(models.Model):
    id = models.AutoField(primary_key=True, db_index=True, verbose_name='类型唯一标识符')
    type = models.CharField(max_length=100, null=False, verbose_name='类型')
    content = models.CharField(max_length=100, null=False, verbose_name='内容')


"""
---------------------------------------------------------消息模型-----------------------------------------------------------------
"""


class Message(models.Model):
    id = models.AutoField(primary_key=True, db_index=True, verbose_name='消息唯一标识符')
    title = models.CharField(max_length=100, null=False, verbose_name='标题')
    content = models.CharField(max_length=100, null=False, verbose_name='内容')
    user = models.ForeignKey(to="User", verbose_name='用户唯一标识符', on_delete=models.SET_NULL, null=True,
                             db_index=True)
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='消息创建时间')
    state = models.IntegerField(verbose_name='消息状态', null=False, default=0)
